<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="姓名或账户" v-model="listQuery.name"> </el-input>
      <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" v-if="genTableManager_btn_add" style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
    </div>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">

      <el-table-column align="center" label="table_name" width="65">
      <template scope="scope">
        <span>{{scope.row.tableName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="实体类名称">
      <template scope="scope">
        <span>{{scope.row.className}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="表说明">
      <template scope="scope">
        <span>{{scope.row.comments}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="关联父表的表名">
      <template scope="scope">
        <span>{{scope.row.parentTableName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="本表关联父表的外键名">
      <template scope="scope">
        <span>{{scope.row.parentTableFkName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="数据源名称">
      <template scope="scope">
        <span>{{scope.row.dataSourceName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="使用的模板">
      <template scope="scope">
        <span>{{scope.row.tplCategory}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成包路径">
      <template scope="scope">
        <span>{{scope.row.packageName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成模块名">
      <template scope="scope">
        <span>{{scope.row.moduleName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成子模块名">
      <template scope="scope">
        <span>{{scope.row.subModuleName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成功能名">
      <template scope="scope">
        <span>{{scope.row.functionName}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成功能名（简写）">
      <template scope="scope">
        <span>{{scope.row.functionNameSimple}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成功能作者">
      <template scope="scope">
        <span>{{scope.row.functionAuthor}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="生成基础路径">
      <template scope="scope">
        <span>{{scope.row.genBaseDir}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="其它生成选项">
      <template scope="scope">
        <span>{{scope.row.options}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="创建者">
      <template scope="scope">
        <span>{{scope.row.createBy}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="创建时间">
      <template scope="scope">
        <span>{{scope.row.createDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="更新者">
      <template scope="scope">
        <span>{{scope.row.updateBy}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="更新时间">
      <template scope="scope">
        <span>{{scope.row.updateDate}}</span>
      </template>
    </el-table-column>
        <el-table-column width="200px" align="center" label="备注信息">
      <template scope="scope">
        <span>{{scope.row.remarks}}</span>
      </template>
    </el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="150"> <template scope="scope">
        <el-button v-if="genTableManager_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-if="genTableManager_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="实体类名称" prop="className">
      <el-input v-model="form.className" placeholder="请输入实体类名称"></el-input>
    </el-form-item>
        <el-form-item label="表说明" prop="comments">
      <el-input v-model="form.comments" placeholder="请输入表说明"></el-input>
    </el-form-item>
        <el-form-item label="关联父表的表名" prop="parentTableName">
      <el-input v-model="form.parentTableName" placeholder="请输入关联父表的表名"></el-input>
    </el-form-item>
        <el-form-item label="本表关联父表的外键名" prop="parentTableFkName">
      <el-input v-model="form.parentTableFkName" placeholder="请输入本表关联父表的外键名"></el-input>
    </el-form-item>
        <el-form-item label="数据源名称" prop="dataSourceName">
      <el-input v-model="form.dataSourceName" placeholder="请输入数据源名称"></el-input>
    </el-form-item>
        <el-form-item label="使用的模板" prop="tplCategory">
      <el-input v-model="form.tplCategory" placeholder="请输入使用的模板"></el-input>
    </el-form-item>
        <el-form-item label="生成包路径" prop="packageName">
      <el-input v-model="form.packageName" placeholder="请输入生成包路径"></el-input>
    </el-form-item>
        <el-form-item label="生成模块名" prop="moduleName">
      <el-input v-model="form.moduleName" placeholder="请输入生成模块名"></el-input>
    </el-form-item>
        <el-form-item label="生成子模块名" prop="subModuleName">
      <el-input v-model="form.subModuleName" placeholder="请输入生成子模块名"></el-input>
    </el-form-item>
        <el-form-item label="生成功能名" prop="functionName">
      <el-input v-model="form.functionName" placeholder="请输入生成功能名"></el-input>
    </el-form-item>
        <el-form-item label="生成功能名（简写）" prop="functionNameSimple">
      <el-input v-model="form.functionNameSimple" placeholder="请输入生成功能名（简写）"></el-input>
    </el-form-item>
        <el-form-item label="生成功能作者" prop="functionAuthor">
      <el-input v-model="form.functionAuthor" placeholder="请输入生成功能作者"></el-input>
    </el-form-item>
        <el-form-item label="生成基础路径" prop="genBaseDir">
      <el-input v-model="form.genBaseDir" placeholder="请输入生成基础路径"></el-input>
    </el-form-item>
        <el-form-item label="其它生成选项" prop="options">
      <el-input v-model="form.options" placeholder="请输入其它生成选项"></el-input>
    </el-form-item>
        <el-form-item label="创建者" prop="createBy">
      <el-input v-model="form.createBy" placeholder="请输入创建者"></el-input>
    </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
      <el-input v-model="form.createDate" placeholder="请输入创建时间"></el-input>
    </el-form-item>
        <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="form.updateBy" placeholder="请输入更新者"></el-input>
    </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
      <el-input v-model="form.updateDate" placeholder="请输入更新时间"></el-input>
    </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
      <el-input v-model="form.remarks" placeholder="请输入备注信息"></el-input>
    </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
        <el-button v-else type="primary" @click="update('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
      page,
      addObj,
      getObj,
      delObj,
      putObj
  } from 'api/generator/genTable/index';
  import { mapGetters } from 'vuex';
  export default {
    name: 'genTable',
    data() {
      return {
        form: {
        className : undefined,        comments : undefined,        parentTableName : undefined,        parentTableFkName : undefined,        dataSourceName : undefined,        tplCategory : undefined,        packageName : undefined,        moduleName : undefined,        subModuleName : undefined,        functionName : undefined,        functionNameSimple : undefined,        functionAuthor : undefined,        genBaseDir : undefined,        options : undefined,        createBy : undefined,        createDate : undefined,        updateBy : undefined,        updateDate : undefined,        remarks : undefined          },
        rules: {
    className: [
  {
    required: true,
    message: '请输入实体类名称',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   comments: [
  {
    required: true,
    message: '请输入表说明',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   parentTableName: [
  {
    required: true,
    message: '请输入关联父表的表名',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   parentTableFkName: [
  {
    required: true,
    message: '请输入本表关联父表的外键名',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   dataSourceName: [
  {
    required: true,
    message: '请输入数据源名称',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   tplCategory: [
  {
    required: true,
    message: '请输入使用的模板',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   packageName: [
  {
    required: true,
    message: '请输入生成包路径',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   moduleName: [
  {
    required: true,
    message: '请输入生成模块名',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   subModuleName: [
  {
    required: true,
    message: '请输入生成子模块名',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   functionName: [
  {
    required: true,
    message: '请输入生成功能名',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   functionNameSimple: [
  {
    required: true,
    message: '请输入生成功能名（简写）',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   functionAuthor: [
  {
    required: true,
    message: '请输入生成功能作者',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   genBaseDir: [
  {
    required: true,
    message: '请输入生成基础路径',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   options: [
  {
    required: true,
    message: '请输入其它生成选项',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   createBy: [
  {
    required: true,
    message: '请输入创建者',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   createDate: [
  {
    required: true,
    message: '请输入创建时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   updateBy: [
  {
    required: true,
    message: '请输入更新者',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   updateDate: [
  {
    required: true,
    message: '请输入更新时间',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
],   remarks: [
  {
    required: true,
    message: '请输入备注信息',
    trigger: 'blur'
  },
  {
    min: 3,
    max: 20,
    message: '长度在 3 到 20 个字符',
    trigger: 'blur'
  }
]        },
        list: null,
        total: null,
        listLoading: true,
        listQuery: {
          page: 1,
          limit: 20,
          name: undefined
        },
        dialogFormVisible: false,
        dialogStatus: '',
        genTableManager_btn_edit: false,
        genTableManager_btn_del: false,
        genTableManager_btn_add: false,
        textMap: {
          update: '编辑',
          create: '创建'
        },
        tableKey: 0
      }
    },
    created() {
      this.getList();
      this.genTableManager_btn_edit = this.elements['genTableManager:btn_edit'];
      this.genTableManager_btn_del = this.elements['genTableManager:btn_del'];
      this.genTableManager_btn_add = this.elements['genTableManager:btn_add'];
    },
    computed: {
      ...mapGetters([
        'elements'
      ])
    },
    methods: {
      getList() {
        this.listLoading = true;
        page(this.listQuery)
            .then(response => {
          this.list = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      })
      },
      handleFilter() {
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.limit = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleCreate() {
        this.resetTemp();
        this.dialogStatus = 'create';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        getObj(row.id)
            .then(response => {
          this.form = response.data;
        this.dialogFormVisible = true;
        this.dialogStatus = 'update';
      });
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
            .then(() => {
          delObj(row.id)
      .then(() => {
          this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        });
        const index = this.list.indexOf(row);
        this.list.splice(index, 1);
      });
      });
      },
      create(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        addObj(this.form)
        .then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        })
        } else {
        return false;
        }
        });
      },
      cancel(formName) {
        this.dialogFormVisible = false;
        const set = this.$refs;
        set[formName].resetFields();
      },
      update(formName) {
        const set = this.$refs;
        set[formName].validate(valid => {
        if (valid) {
        this.dialogFormVisible = false;
        this.form.password = undefined;
        putObj(this.form.id, this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
        title: '成功',
        message: '创建成功',
        type: 'success',
        duration: 2000
        });
        });
        } else {
        return false;
        }
        });
      },
      resetTemp() {
        this.form = {
          username: undefined,
          name: undefined,
          sex: '男',
          password: undefined,
          description: undefined
        };
      }
    }
  }
</script>
